package com.guli.eduService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.eduService.entity.EduSubject;
import com.guli.eduService.entity.excel.SubjectData;
import com.guli.eduService.entity.subject.OneSubject;
import com.guli.eduService.entity.subject.TwoSubject;
import com.guli.eduService.listener.SubjectExcelListener;
import com.guli.eduService.mapper.EduSubjectMapper;
import com.guli.eduService.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author LMZovo
 * @since 2022-02-15
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * 添加课程分类
     */
    @Override
    public void saveSubject(MultipartFile multipartFile) {
        InputStream inputStream = null;
        try {
            inputStream = multipartFile.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(this))
                    .sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 返回所有分类
     */
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", 0);
        List<EduSubject> OneSubjectList = baseMapper.selectList(wrapperOne);//父类中对mapper进行了注入
        //查询二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", 0);
        List<EduSubject> TwoSubjectList = baseMapper.selectList(wrapperTwo);//父类中对mapper进行了注入

        //封装数据
        List<OneSubject> OneResult = new ArrayList<>();
        for (int i = 0; i < OneSubjectList.size(); i++) {
            //取出OneSubjectList中的值
            EduSubject OneEduSubject = OneSubjectList.get(i);
            //将得到的值设置到oneSubject对象中
            OneSubject oneSubject = new OneSubject();
            //oneSubject.setId(eduSubject.getId());
            //oneSubject.setTitle(eduSubject.getTitle());
            //使用spring的工具类
            BeanUtils.copyProperties(OneEduSubject, oneSubject);
            OneResult.add(oneSubject);
            //将二级分类的数据封装到一级分类的对象中
            List<TwoSubject> twoResult = new ArrayList<>();
            for (int j = 0; j < TwoSubjectList.size(); j++) {
                EduSubject TwoEduSubject = TwoSubjectList.get(j);
                //判断二级分类的parent_id是否与一级分类的id相同
                String parentId = TwoEduSubject.getParentId();
                String id = OneEduSubject.getId();
                //二级分类的pid与一级分类的id是否匹配
                if (parentId.equals(id)) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(TwoEduSubject, twoSubject);
                    twoResult.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoResult);
        }
        return OneResult;
    }
}